﻿using System;
using System.Net.Sockets;

namespace HPSocket
{
    /// <summary>
    /// Socket数据缓冲区
    /// </summary>
    public class SocketDataBuffer : IDisposable
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        /// <param name="bufferSize"></param>
        public SocketDataBuffer(Socket client, int bufferSize = 1024)
        {
            Client = client;
            BufferSize = bufferSize;
            Buffer = new byte[BufferSize];
        }

        public Socket Client { get; private set; }
        public byte[] Buffer { get; private set; }
        public int BufferSize { get; private set; }
        public int Offset { get; set; }
        public int Length { get; set; }

        #region
        private bool disposedValue;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    Client?.Dispose();
                }
                Client = null;
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~SocketDataBuffer()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        #endregion

    }
}